package com.mengdie.shuidi.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.mengdie.shuidi.AppContext;
import com.mengdie.shuidi.OkHeader;
import com.mengdie.shuidi.R;
import com.mengdie.shuidi.model.ChildMsg;
import com.orhanobut.logger.Logger;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class SystemMsgDescActivity extends BaseActivity {
    private static final String TAG = "SystemMsgDescActivity";

    @Bind(R.id.tv_title)
    TextView mTvTitle;
    @Bind(R.id.tv_desc)
    TextView mTvDesc;
    @Bind(R.id.tv_date)
    TextView mTvDate;
    @Bind(R.id.rl_generic_back)
    RelativeLayout mRlGenericBack;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;

    public static Intent newIntent(ChildMsg data) {
        Intent intent = new Intent();
        intent.setClass(AppContext.getInstance(), SystemMsgDescActivity.class);
        Bundle b = new Bundle();
        b.putSerializable(TAG, data);
        intent.putExtras(b);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_msg_desc);
        ButterKnife.bind(this);

        mTvGenericTitle.setText("系统消息");
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        ChildMsg data = (ChildMsg) intent.getSerializableExtra(TAG);
        mTvTitle.setText(data.getTitle());
        mTvDesc.setText(data.getContent());
        mTvDate.setText(data.getSend_date());

        setRead(data);

    }

    private void setRead(ChildMsg data) {
        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this))
                .post(AppContext.getInstance().getBaseUrl()+"ucenter/read_flag")
                .params("msg_id",data.getMsgId())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Logger.e(s);
                        try {
                            JSONObject jo = new JSONObject(s);
                            String ret = jo.getString("ret");
                            String code = jo.getString("code");
                            String msg = jo.getString("msg");
                            if (ret.equals("0") && code.equals("1")) {
                                //ToastUtil.showToast("标记已读成功");
                            } else if (ret.equals("1") && code.equals("-1")) {
                                // ToastUtil.showToast("标记已读失败");
                            }
                        } catch (Exception e) {
                        }
                    }
                });
    }

    @OnClick(R.id.rl_generic_back)
    public void onClick() {
        finish();
    }
}
